from twisted.enterprise import adbapi
import pymysql

# 异步更新操作
class moviePipeline(object):
    def __init__(self, dbpool):
        self.dbpool = dbpool
 
    @classmethod
    def from_settings(cls, settings):  # 函数名固定，会被scrapy调用，直接可用settings的值
        """
        数据库建立连接
        :param settings: 配置参数
        :return: 实例化参数
        """
        adbparams = dict(
            host='127.0.0.1',
            db='movielist',
            user='root',
            password='',
            cursorclass=pymysql.cursors.DictCursor   # 指定cursor类型
        )
 
        # 连接数据池ConnectionPool，使用pymysql或者Mysqldb连接
        dbpool = adbapi.ConnectionPool('pymysql', **adbparams)
        # 返回实例化参数
        return cls(dbpool)
 
    def process_item(self, item, spider):
        """
        使用twisted将MySQL插入变成异步执行。通过连接池执行具体的sql操作，返回一个对象
        """
        query = self.dbpool.runInteraction(self.do_insert, item)  # 指定操作方法和操作数据
        # 添加异常处理
        query.addCallback(self.handle_error)  # 处理异常
 
    def do_insert(self, cursor, item):
        # 对数据库进行插入操作，并不需要commit，twisted会自动commit
        insert_sql = """
        insert into movie_t(id, movie_title, movie_img, movie_type, movie_city,movie_update) VALUES (null,%s,%s,%s,%s,%s)
        """
        cursor.execute(insert_sql, (item['movie_title'], item['movie_img'], item['movie_type'], item['movie_city'],
                                                  item['movie_update']))

        #将数据 插入电影详情
        insert_sql2 = """
        insert into movie_detail(m_id, movie_info, movie_m3u8, movie_zuidal, movie_xunlei) VALUES (null,%s,%s,%s,%s)
        """
        cursor.execute(insert_sql2, (item['movie_info'], str(item['movie_m3u8']), str(item['movie_zuidal']), str(item['movie_xunlei'])))

    def handle_error(self, failure):
        if failure:
            # 打印错误信息
            print(failure)
